package com.miracle9.game.server.message;

import com.miracle9.game.bean.DeskSeatBean;
import com.miracle9.game.service.UserService;
import com.miracle9.game.service.XsManageService;
import com.miracle9.game.service.XxFishService;
import org.apache.log4j.Logger;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class MainServerH5 {
    private Logger logger = Logger.getLogger(MainServerH5.class);
    private static MainServerH5 mainServer = null;
    private SocketAcceptor acceptor = new NioSocketAcceptor();
    private DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

    private int bindPort = 6992;



    @Autowired
    private UserService userService;
    @Autowired
    private XxFishService xxFishService;
    @PostConstruct
    public void init(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                logger.info("h5 start .....");
                acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);

                // 设置编码过滤器
                acceptor.getFilterChain().addLast("coder",
                     //   new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
                   //     new ProtocolCodecFilter(new CharsetCodecFactory()));
                       // new ProtocolCodecFilter(new WebSocketCodecFactory()));
                    //    new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
                        new ProtocolCodecFilter(new MinaEncoder(),new MinaDecoder()));
                acceptor.setHandler(new MinaServerHandler(userService,xxFishService));
                try {
                    acceptor.bind(new InetSocketAddress(bindPort));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

}

